/******************************************************************************
 * FileName:    gwin_motor_task.c
 * Desc:        电机任务
 *
 *
 * Author:      Lin
 * Notes:
 *
 ******************************************************************************/

/*-------------------------- Includes ---------------------------------------*/
#include "gwin_motor_task.h"

#include "gwin_main_task.h"


/*-------------------------- Macro Definitions ------------------------------*/
#define LOCAL_DEBUG_EN                      ( 0 )
#if LOCAL_DEBUG_EN
#define GWIN_DEBUG(...)                     USER_DEBUG(__VA_ARGS__)
#else
#define GWIN_DEBUG(...)
#endif


/*-------------------------- Type Declarations ------------------------------*/

/*-------------------------- Global Variables -------------------------------*/

/*-------------------------- Local Variables --------------------------------*/
static void *gwin_motor_task_timer_gradual = NULL;

static motor_target_t motor_target = { 0x00 };          // 电机 目标渐变控制


/*-------------------------- Functions Declarations -------------------------*/
static void gwin_motor_task(void *arg);
static void gwin_motor_gradual_callback(void *arg);

static void gwin_motor_set_motor_pwm(void);

static void gwin_motor_task_cfg_msg_handle(gwin_motor_task_msg_t *msg);
static void gwin_motor_task_timer_msg_handle(gwin_motor_task_msg_t *msg);
static void gwin_motor_task_motor_msg_handle(gwin_motor_task_msg_t *msg);


/*-------------------------- Functions Definitions --------------------------*/
/**
 * @brief 电机渐变开始
 */
void gwin_motor_run_start(uint8_t target_position)
{
    USER_DEBUG("target_position = %d", target_position);

    gwin_motor_task_msg_t msg = { 0 };
    msg.type = MOTOR_TASK_MSG_TYPE_MOTOR;
    msg.subtype = MOTOR_TASK_MSG_MOTOR_SUBTYPE_START;
    msg.target_position = target_position;
    gwin_send_msg_to_motor_task(&msg);
}

/**
 * @brief 电机渐变停止
 */
void gwin_motor_gradual_stop(void)
{
    GWIN_DEBUG("");

    gwin_motor_task_msg_t msg = { 0 };
    msg.type = MOTOR_TASK_MSG_TYPE_MOTOR;
    msg.subtype = MOTOR_TASK_MSG_MOTOR_SUBTYPE_STOP;
    gwin_send_msg_to_motor_task(&msg);
}


/**
 * @brief 电机 设置PWM
 */
static void gwin_motor_set_motor_pwm(void)
{
    uint32_t pwm_motor_1 = 0;
    uint32_t pwm_motor_2 = 0;

    // 设置占空比
    gwin_motor_set_pwm(0, pwm_motor_1);
    gwin_motor_set_pwm(1, pwm_motor_2);
}

/**
 * @brief 电机 当前值渐变到目标值
 */
static void gwin_motor_gradual(void)
{
    // 设置PWM
    gwin_motor_set_motor_pwm();
//    os_timer_start(gwin_motor_task_timer_gradual, 0);
}



/**
 * @brief 设置消息处理
 */
static void gwin_motor_task_cfg_msg_handle(gwin_motor_task_msg_t *msg)
{
    uint16_t subtype = msg->subtype;
    uint8_t action_type = msg->action_type;

    if (action_type != MOTOR_TASK_MSG_CFG_ACT_TYPE_SET
            && action_type != MOTOR_TASK_MSG_CFG_ACT_TYPE_GET)
    {
        GWIN_DEBUG("Error : cfg action type none!!!");
        return;
    }
    if (action_type == MAIN_TASK_MSG_CFG_ACT_TYPE_GET && msg->cfg_value == 0)           // 获取的时候，传了空指针，会死机
    {
        GWIN_DEBUG("Error : cfg action get %d pointer is null!!!", subtype);
        return;
    }

    GWIN_DEBUG("subtype = %d, action_type = %d, cfg_value = %d", subtype, action_type, msg->cfg_value);

    switch(subtype)
    {
        default:
            GWIN_DEBUG("Error : subtype of cfg msg is unknown %d !!!", subtype);
            break;
    }
}

/**
 * @brief 定时器消息处理
 */
static void gwin_motor_task_timer_msg_handle(gwin_motor_task_msg_t *msg)
{
    uint16_t subtype = msg->subtype;

    switch (subtype)
    {
        case MOTOR_TASK_MSG_TIMER_SUBTYPE_GRADUAL:
            gwin_motor_gradual();
            break;
    }
}

/**
 * @brief 电机消息处理
 */
static void gwin_motor_task_motor_msg_handle(gwin_motor_task_msg_t *msg)
{
    uint16_t subtype = msg->subtype;

    switch(subtype)
    {
        case MOTOR_TASK_MSG_MOTOR_SUBTYPE_START:
            {
                os_timer_stop(gwin_motor_task_timer_gradual, 0);

                motor_target.state = 1;
                os_timer_start(gwin_motor_task_timer_gradual, 0);
            }
            break;

        case MOTOR_TASK_MSG_MOTOR_SUBTYPE_STOP:
            {
                if (motor_target.state)
                {
                    motor_target.state = 0;
                    os_timer_stop(gwin_motor_task_timer_gradual, 0);

                    // 通知主任务
                }
            }
            break;

        default:
            GWIN_DEBUG("Error : subtype of motor msg is unknown %d !!!", subtype);
            break;
    }
}



/**
 * @brief 任务初始化
 */
void gwin_motor_task_init(void)
{
    gwin_motor_task_timer_gradual = os_timer_create("gwin_motor_gradual", 10, FALSE, 0, gwin_motor_gradual_callback);
    GWIN_OS_TIMER_ASSERT(gwin_motor_task_timer_gradual);

    gwin_motor_task_msg_t msg = { 0 };
    msg.type = MOTOR_TASK_MSG_TYPE_TASK_INIT;
    gwin_send_msg_to_motor_task(&msg);
}

/**
 * @brief 定时器 电机渐变
 */
static void gwin_motor_gradual_callback(void *arg)
{
    gwin_motor_task_msg_t msg = { 0 };
    msg.type = MOTOR_TASK_MSG_TYPE_TIMER;
    msg.subtype = MOTOR_TASK_MSG_TIMER_SUBTYPE_GRADUAL;
    gwin_send_msg_to_motor_task(&msg);
}

/**
 * @brief 任务
 */
static void gwin_motor_task(void *arg)
{
    gwin_motor_task_msg_t *msg = arg;

    switch(msg->type)
    {
        case MOTOR_TASK_MSG_TYPE_TASK_INIT:
            break;

        case MOTOR_TASK_MSG_TYPE_CFG:
            gwin_motor_task_cfg_msg_handle(msg);
            break;

        case MOTOR_TASK_MSG_TYPE_TIMER:
            gwin_motor_task_timer_msg_handle(msg);
            break;

        case MOTOR_TASK_MSG_TYPE_MOTOR:
            gwin_motor_task_motor_msg_handle(msg);
            break;

        default:
            break;
    }
}



/**
 * @brief 电机 编码器
 */
void gwin_motor_encoder_handle(void)
{
    // TODO : 计算电机编码器步进
    gwin_encoder_get_state(0);
    gwin_encoder_get_state(1);
}


/*-------------------------- END OF FILE ------------------------------------*/

